#pragma once

#include <iostream>
#include<vector>
#include<functional>

using namespace std;

namespace aj
{
    template<class T>
    class less
    {
    public:
        bool operator()(const T& x1, const T& x2)
        {
            return x1 < x2;
        }
    };

    template<class T>
    class greater
    {
    public:
        bool operator()(const T& x1, const T& x2)
        {
            return x1 > x2;
        }
    };

    template <class T, class Container = vector<T>, class Compare = less<T> >
    class priority_queue
    {
    public:
        priority_queue() 
        {};

        template <class InputIterator>
        priority_queue(InputIterator first, InputIterator last)
            :c(first,last)
        {
            for (int i = (size() - 2) / 2; i > 0; --i)
            {
                adjust_up(i);
            }
        }

        bool empty() const
        {
            return c.empty();
        }

        size_t size() const
        {
            return c.size();
        }

        const T& top() const
        {
            return c[0];
        }

        void adjust_up(int child)
        {
            int parent = (child - 1) / 2;
            while (child > 0)
            {
                if (comp(c[parent] , c[child]))
                {
                    swap(c[child], c[parent]);
                    child = parent;
                    parent = (child - 1) / 2;
                }
                else
                {
                    break;
                }
            }
        }

        void adjust_down(int parent)
        {
            size_t child = parent * 2 + 1;
            while (child < c.size())
            {
                if (child + 1 < c.size() && comp(c[child] , c[child + 1]))
                {
                    child++;
                }

                // if (c[parent] < c[child])
                if (comp(c[parent] , c[child]))
                {
                    swap(c[child], c[parent]);
                    parent = child;
                    child = parent * 2 + 1;
                }
                else
                {
                    break;
                }
            }
        }

        void push(const T& x)
        {
            c.push_back(x);
            adjust_up(c.size() - 1);
        }

        void pop()
        {
            swap(c[0], c[c, size() - 1]);
            c.pop_back();
            adjust_down(0);
        }

    private:
        Container c;
        Compare comp;
    };

    void test_priority_queue1()
    {
        priority_queue<int> pq;
        pq.push(1);
        pq.push(2);
        pq.push(3);
        pq.push(4);

        while (!pq.empty())
        {
            // cout << pq.size() << ' ';

            cout << pq.top() << ' ';
            pq.pop();
        }
    }

    void test_priority_queue2()
    {
        string s("I Love You");
        priority_queue<int> pq(s.begin(),s.end());

        while (!pq.empty())
        {
            // cout << pq.size() << ' ';

            cout << pq.top() << ' ';
            pq.pop();
        }
    }
};